<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete; // 软删除traits

/**
 * @mixin think\Model
 */
class Content extends Model
{
    // 软删除
    use SoftDelete;
    // 软删除标记位
    protected $deleteTime = 'delete_time';
    protected $append = ['categoryinfo'];
    protected $type = [
        'category'    =>  'integer',
    ];
    // json字段
	protected $json = ['images'];
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;
	// 修改器：从images字段获取封面Cover字段
	public function setCoverAttr($value,$data) {
		if (isset($data['images']) && is_array($data['images'])) {
			return $data['images'][0] || 'ddd';
		}else{
			return 'errr';
		}
	}
    public function categoryinfo()
    {
        return $this->hasOne(Category::class,'cid','category');
    }
    public function getTagsAttr($value,$data) {
        return empty($value) ? [] : explode(',',$value);
    }
    public function setTagsAttr($value,$data) {
        return !is_array($value) ? [] : implode(',',$value);
    }
    public function setCategoryAttr($value,$data) {
        return (empty($value) || !is_array($value)) ? $value : implode(',',$value);
    }
    public function getStatistics() {
        $res = [];
        $res[] = ['category'=> '分类1','count'=>110];
        $res[] = ['category'=> '分类11','count'=>220];
        $res[] = ['category'=> '分类111','count'=>330];
        $res[] = ['category'=> '分类1111','count'=>330];
        $res[] = ['category'=> '分类11111','count'=>440];
        return $res;
    }
    public function getStatusCount(){
        return $this->field('status, count(*) as count')->group('status')->column('count(*)','status');
    }
}
